R Markdown is an authoring format that enables easy creation of dynamic documents, presentations, and reports from R. It combines the core syntax of markdown (an easy-to-write plain text format) with embedded R code chunks that are run so their output can be included in the final document. R Markdown documents are fully reproducible (they can be automatically regenerated whenever underlying R code or data changes).
Study the first page of the R Markdown Reference Guide.
Yes, the entire markdown syntax can be described in one page!
Can you think of anything that is missing from the syntax (that you might want when creating documents)?
LaTeX markup, but don’t expect it to convert between output formats.Have a look at R Markdown presentations and templates.
Pro tip: run devtools::install_github("rstudio/rticles") to get more templates
The stuff at the top of the .Rmd file (called yaml front matter) tells rmarkdown what output format you want.
---
title: "Untitled"
date: "May 16, 2016"
output: html_document
---
In this case, when you click “Knit HTML”, RStudio calls rmarkdown::render("file.Rmd", html_document()). You can certainly change these default values (see the source of this presentation).
A code chunk is a concept borrowed from the knitr package (which, in turn, was inspired by literate programming). In .Rmd files, you can start/end a code chunk with three back-ticks.
1 + 1
## [1] 2Want to run a command in another language?
print "a" + "b"
## abThere are a plethora of chunk options in knitr (engine is one of them). Here are some that I typically use:
echo: Show the code?eval: Run the code?message: Relay messages?warning: Relay warnings?fig.width and fig.height: Change size of figure output.cache: Save the output of this chunk (so we don’t have to run it next time)?Study the second page of the R Markdown Reference Guide and go back to the Hello R Markdown example we created.
Easy: Modify the figure sizing and alignment.
Medium: Add a figure caption.
Hard: Can you create an animation? (Hint: look at the fig.show chunk option – you might need to the animation package for this)
Pro Tip: Don’t like the default chunk option value? Change it at the top of the document:
knitr::opts_chunk$set(message = FALSE, warning = FALSE)m <- lm(mpg ~ disp, data = mtcars)
summary(m) # output isn't very attractive
##
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.8922 -2.2022 -0.9631 1.6272 7.2305
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.599855 1.229720 24.070 < 2e-16 ***
## disp -0.041215 0.004712 -8.747 9.38e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.251 on 30 degrees of freedom
## Multiple R-squared: 0.7183, Adjusted R-squared: 0.709
## F-statistic: 76.51 on 1 and 30 DF, p-value: 9.38e-10pander is one great option.
library(pander)
pander(m)| Â | Estimate | Std. Error | t value | Pr(>|t|) |
|---|---|---|---|---|
| disp | -0.04122 | 0.004712 | -8.747 | 9.38e-10 |
| (Intercept) | 29.6 | 1.23 | 24.07 | 3.577e-21 |
a <- anova(m)
a
## Analysis of Variance Table
##
## Response: mpg
## Df Sum Sq Mean Sq F value Pr(>F)
## disp 1 808.89 808.89 76.513 9.38e-10 ***
## Residuals 30 317.16 10.57
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1pander(a)| Â | Df | Sum Sq | Mean Sq | F value | Pr(>F) |
|---|---|---|---|---|---|
| disp | 1 | 808.9 | 808.9 | 76.51 | 9.38e-10 |
| Residuals | 30 | 317.2 | 10.57 | NA | NA |
methods(pander)
## [1] pander.anova* pander.aov*
## [3] pander.aovlist* pander.call*
## [5] pander.cast_df* pander.character*
## [7] pander.clogit* pander.coxph*
## [9] pander.CrossTable* pander.data.frame*
## [11] pander.Date* pander.default*
## [13] pander.density* pander.describe*
## [15] pander.evals* pander.factor*
## [17] pander.formula* pander.ftable*
## [19] pander.function* pander.glm*
## [21] pander.htest* pander.image*
## [23] pander.list* pander.lm*
## [25] pander.lme* pander.logical*
## [27] pander.matrix* pander.microbenchmark*
## [29] pander.mtable* pander.NULL*
## [31] pander.numeric* pander.option
## [33] pander.POSIXct* pander.POSIXlt*
## [35] pander.prcomp* pander.rapport*
## [37] pander.return pander.rlm*
## [39] pander.sessionInfo* pander.smooth.spline*
## [41] pander.stat.table* pander.summary.aov*
## [43] pander.summary.aovlist* pander.summary.glm*
## [45] pander.summary.lm* pander.summary.prcomp*
## [47] pander.survdiff* pander.survfit*
## [49] pander.table* pander.tabular*
## [51] pander.ts* pander.zoo*
## see '?methods' for accessing help and source codepander.lm and pander.anova.